<?php

namespace App\Controller;

use Think\Controller;
use Usap\Model\Data\AesModel;

class ApiController extends Controller
{
    /*
   * 对请求数据解密
   */
    protected function decrypt($paraStr)
    {
        $aesKey = C('CryptAEC.aesKey');//秘钥
        $aesIV = C('CryptAEC.aesIV'); //向量
        $aes = new AesModel($aesKey, $aesIV, array('PKCS7' => true, 'mode' => 'cbc'));
        $decrypt = $aes->decrypt(base64_decode($paraStr));
        $data = json_decode($decrypt, true);
        return $data;
    }

    /*
    * 对返回数据加密
     */
    protected function encrypt($data)
    {
        $aesKey = C('CryptAEC.aesKey');//秘钥
        $aesIV = C('CryptAEC.aesIV'); //向量
        $aes = new AesModel($aesKey, $aesIV, array('PKCS7' => true, 'mode' => 'cbc'));
        $str = json_encode($data);//转成字符串
        $str = $aes->encrypt($str);//aes加密
        return base64_encode($str);

    }

    /**
     * @desc arraySort php二维数组排序 按照指定的keys 对数组进行排序
     * @param array $arr 将要排序的数组
     * @param string $keys 指定排序的字段值
     * @param string $type 排序类型 asc | desc
     * @return array
     */
    public function array_sort($arr, $keys, $type = 'asc')
    {
        $keysvalue = $new_array = [];
        foreach ($arr as $k => $v) {
            $keysvalue[$k] = $v[$keys];
        }
        if ($type == 'asc') {
            asort($keysvalue);
        } else {
            arsort($keysvalue);
        }
        reset($keysvalue);
        foreach ($keysvalue as $k => $v) {
            $new_array[] = $arr[$k];
        }
        return $new_array;
    }
}